﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Domain.PermisosModule.Entities;
using System.ComponentModel;
using Cesave.Domain.PermisosModule.Contracts.Modulos;
using Cesave.Infrastructure.CrossCutting.IoC;
using System.Windows.Forms;
using Cesave.Domain.Core.Entities;

namespace Cesave.DistributedServices.PermisosModule.WindowsHost.ViewModels.Catalogs.Modulos
{
	public partial class ModulosViewModel
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private List<Modulo> _modulos;

		/// <summary>
		/// 
		/// </summary>
		private Modulo _modulo;

		#endregion

		#region Public properties

		/// <summary>
		/// 
		/// </summary>
		public List<Modulo> Modulos
		{
			get
			{
				return _modulos;
			}
			private set
			{
				_modulos = value;
				if (_modulos != null)
					RaisePropertyChanged("Modulos");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public Modulo Modulo
		{
			get
			{
				return _modulo;
			}
			private set
			{
				_modulo = value;
				if (_modulo != null)
					RaisePropertyChanged("Modulo");
			}
		}

		/// <summary>
		/// 
		/// </summary>
		public bool IsModuloReady
		{
			get
			{
				return Modulo != null;
			}
		}

		#endregion


		#region Private methods

		/// <summary>
		/// 
		/// </summary>
		private void LoadModulos()
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (sender, e) =>
				{
					IModuloService service = IoCFactory.Resolve<IModuloService>();
					e.Result = service.FindModulosByJunta(Junta.JuntaGuid);
				};
				worker.RunWorkerCompleted += (sender, e) =>
				{
					EndWaiting();
					if (!e.Cancelled && e.Error == null)
						Modulos = e.Result as List<Modulo>;
					else if (e.Error != null)
						ShowMessage(e.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddNewModulo(object sender, EventArgs e)
		{
			_view.modulosBindingSource.AddingNew += AddingNewModulo;
			_view.modulosBindingSource.AddNew();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void AddingNewModulo(object sender, AddingNewEventArgs e)
		{
			Modulo = new Modulo
			{
				DistritoGuid = DistritoModulo.DistritoGuid,
				DistritoId = DistritoModulo.DistritoId,
				EstadoGuid = EstadoModulo.EstadoGuid,
				EstadoId = EstadoModulo.EstadoId,
				JuntaGuid = Junta.JuntaGuid,
				JuntaId = Junta.JuntaId,
				ModuloGuid = Guid.NewGuid(),
				ModuloId = 0,
				MunicipioGuid = MunicipioModulo.MunicipioGuid,
				MunicipioId = MunicipioModulo.MunicipioId,
				Nombre = string.Empty,
				Status = true,
			};
			Modulo.MarkAsAdded();
			e.NewObject = Modulo;
			FocusDefaultControl();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SaveModulos(object sender, EventArgs e)
		{
			StartWaiting();
			using (BackgroundWorker worker = new BackgroundWorker())
			{
				worker.DoWork += (s, args) =>
				{
					IModuloService service = IoCFactory.Resolve<IModuloService>();
					Modulos.ForEach(m =>
					{
						if (m.ChangeTracker.State == ObjectState.Added)
							service.AddModulo(m);
						else
							service.ChangeModulo(m);
						m.AcceptChanges();
					});
				};
				worker.RunWorkerCompleted += (s, args) =>
				{
					EndWaiting();
					if (!args.Cancelled && args.Error == null)
						LoadModulos();
					else if (args.Error != null)
						ShowMessage(args.Error.Message);
				};
				worker.WorkerSupportsCancellation = true;
				worker.RunWorkerAsync();
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void SetCurrentModulo(object sender, EventArgs e)
		{
			BindingSource bindingSource = sender as BindingSource;
			if ((bindingSource != null) && (bindingSource.Current != null))
				Modulo = bindingSource.Current as Modulo;
		}

		private void OnModulosPropertyChanged()
		{
			_view.modulosBindingSource.DataSource = Modulos;
			_view.modulosBindingSource.CurrentChanged += SetCurrentModulo;
			SetCurrentModulo(_view.modulosBindingSource, new EventArgs());
		}

		/// <summary>
		/// 
		/// </summary>
		private void OnModuloPropertyChanged()
		{
			if (IsModuloReady)
			{
				_view.estadoGuidComboBox.SelectedValue = Modulo.EstadoGuid;
				_view.distritoGuidComboBox.SelectedValue = Modulo.DistritoGuid;
				_view.municipioGuidComboBox.SelectedValue = Modulo.MunicipioGuid;
				_view.juntaGuidComboBox.SelectedValue = Modulo.JuntaGuid;
			}
		}

		#endregion

	}
}
